<template>
	<view class="share">
		<view class="image-area">
			<view class="section">
				<view class="title">
					{{ name }}
				</view>
				<view>
					<image :src="imageURL" mode=""></image>
				</view>
			</view>
		</view>
		
		<view class="btn-area">
			<button class='bottom' type='primary' open-type="share">商品分享</button>
		</view>
	</view>
</template>

<script>
	import Storage from '@/common/scripts/storage.js'
	import IndexService from './service/service.js'
	
	export default {
		name: 'share',
		beforeCreate() {
			this.service = new IndexService()
		},
		data() {
			return {
				imageURL: '',
				path: '',
				goodId: '',
				goods: '',
				name: '',
				price: ''
			}
		},
		onLoad(options) {
			this.goodId = options.goods_id
			delete options['goods_id']
			this.options = options
			Storage.set('back-page', options)
			this.init()
		},
		methods: {
			init() {
				let that = this
				that.service.getGoods({
					'goods_id': that.goodId
				}).then(that.getGoodSuccess).catch(that.getGoodError)
			},
			getGoodSuccess(result) {
				let that = this
				console.log(result)
				if (result) {
					that.imageURL = result.image
					that.name = result.name
					that.price = result.price
				} else {
					uni.showModal({
						title: '错误提示',
						content: '获取商品信息失败！',
						success: function(res) {
							that.toIndex()
						},
						duration: 3000
					})
				}
			},
			getGoodError(err) {
				let that = this
				uni.showModal({
					title: '错误提示',
					content: '获取商品信息失败！',
					success: function(res) {
						that.toIndex()
					},
					duration: 3000
				})
			},
			toIndex() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			shareSuccess(res) {
				let that = this
				// 转发成功之后的回调
				if (res.errMsg == 'shareAppMessage:ok') {
					uni.showModal({
						title: '转发提示',
						content: '商品转发成功！',
						success: function(res) {
							that.toIndex()
						},
						duration: 3000
					})
				}
			},
			shareError(res) {
				uni.showModal({
					title: '转发提示',
					content: '商品转发失败！',
					success: function(res) {
						console.log(res)
					},
					duration: 3000
				})
				// 转发失败之后的回调
				if (res.errMsg == 'shareAppMessage:fail cancel') {
					// 用户取消转发
				} else if (res.errMsg == 'shareAppMessage:fail') {
					// 转发失败，其中 detail message 为详细失败信息
				}
			},
			onShareAppMessage(options) {
				var that = this;
				// 设置菜单中的转发按钮触发转发事件时的转发内容
				
				var shareObj = {
					title: that.name, // 默认是小程序的名称(可以写slogan等)
					path: '/pages/index/index?g=Wap&c=Mall&a=detail&lj_page=wap.php&rt=share&goods_id=' + this.goodId, // 默认是当前页面，必须是以‘/’开头的完整路径
					desc: that.price,
					imageUrl: that.imageURL, //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
					success: that.shareSuccess,
					fail: that.shareError
				}
				// 返回shareObj
				return shareObj;
			}
		}
	}
</script>

<style>
	.bottom {
		border-radius: 80upx;
		margin: 70upx 50upx;
		font-size: 35upx;
	}
	.share {
		height: 100%;
		display: flex;
		flex-direction: column;
	}
	.image-area {
		margin-top: 80upx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 4upx;
	}
	.image-area .section {
		background: #FFFFFF;
		border-radius: 10upx;
		border: 1upx solid #DDDDDD;
		box-shadow: 0 0 30upx rgba(220,220,220,0.7);
		padding: 10upx;
	}
	.image-area .section .title {
		font-weight: 600;
		padding: 20upx;
	}
	.btn-area {
		
	}
</style>
